package com.wuye.service;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.wuye.dao.JiaofeiDaoImpl;
import com.wuye.dao.UsersDaoImpl;
import com.wuye.dao.WeixiuDaoImpl;
import com.wuye.entity.Jiaofei;
import com.wuye.entity.Weixiu;

@Service("jiaofeiService")
public class JiaofeiService {
	@Resource
	private JiaofeiDaoImpl jiaofeiDao;
	@Resource
	private UsersDaoImpl userDao;

	public void save(Jiaofei jiaofei) {
		jiaofei.setShijian(new Date());
		jiaofeiDao.save(jiaofei);
	}

	public void del(String str) {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Jiaofei jiaofei = new Jiaofei();
			jiaofei.setId(Integer.parseInt(sz[i]));
			jiaofeiDao.del(jiaofei);
		}
	}

	public void update(String str) {
		String[] sz = str.split(",");
		for (int i = 0; i < sz.length; i++) {
			Jiaofei jiaofei  = jiaofeiDao.getObject(Integer.parseInt(sz[i]));
			jiaofei.setShijian(new Date());
			jiaofeiDao.update(jiaofei);
		}
	}

	public Map getList(int offset, int length) {
		String[] title = { "id", "xiangmu", "money","shijian", "username" };
		return jiaofeiDao
				.getlist(
						title,
						"select j.id,j.xiangmu,j.money,substring(j.shijian,1,10),j.users.name from Jiaofei j",
						offset, length);
	}

	public Map getSearchList(Jiaofei jiaofei, int offset, int length,String shijian1,String shijian2)
			throws Exception {
		StringBuffer sb = new StringBuffer();
		String[] title = { "id", "xiangmu", "money","shijian", "username" };
		sb.append("select j.id,j.xiangmu,j.money,substring(j.shijian,1,10),j.users.name from Jiaofei j ");
		if(!shijian1.equals("")){
			sb.append(" where j.shijian >='"+shijian1+"'");
			if(!shijian2.equals("")){
				sb.append(" and j.shijian <='"+shijian2+"'");
			}
		}else{
			sb.append(" where j.shijian <='"+shijian2+"'");
		}
		
		return jiaofeiDao.getlist(title, sb.toString(), offset, length);
	}
	public void goPage(HttpServletRequest request){
		request.setAttribute("list", userDao.getComboxList());
	}
	
	public UsersDaoImpl getUserDao() {
		return userDao;
	}

	public void setUserDao(UsersDaoImpl userDao) {
		this.userDao = userDao;
	}

	public JiaofeiDaoImpl getJiaofeiDao() {
		return jiaofeiDao;
	}

	public void setJiaofeiDao(JiaofeiDaoImpl jiaofeiDao) {
		this.jiaofeiDao = jiaofeiDao;
	}
}
